package com.laxcen.toydemo;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.laxcen.controls.HomeMenuButtonControl;
import com.laxcen.intelligenttoy.BLE.ETag;

import util.BackgroundMusic;
import util.DefaultHandler;
import util.DelayHandler;

public class GameActivity extends BaseActivity implements View.OnClickListener {
    GameState mGameState = GameState.instance(this);
    HomeMenuButtonControl btn_menu_learning, btn_menu_game, btn_menu_report, btn_exit_app;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        mGameState.start();
        btn_menu_learning = findViewById(R.id.btn_menu_learning);
        btn_menu_game = findViewById(R.id.btn_menu_game);
        btn_menu_report = findViewById(R.id.btn_menu_report);
        btn_exit_app = findViewById(R.id.btn_exit_app);
        btn_menu_learning.setOnClickListener(this);
        btn_menu_game.setOnClickListener(this);
        btn_menu_report.setOnClickListener(this);
        btn_exit_app.setOnClickListener(this);

        BackgroundMusic.instance(GameActivity.this).play(R.raw.game_connect_success, false);
    }

    private void startRepeatPromptGbSound() {
        new DefaultHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mGameState.getStep() == GameState.EGameStep.AppOpenedGameWait) {
                    BackgroundMusic.instance(GameActivity.this).play(R.raw.guide_repeat_prompt, false);
                }
            }
        }, 2000);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_menu_learning) {
           gotoLearningActivity();
        } else if (v.getId() == R.id.btn_menu_game) {
           gotoGameStepSelectActivity();
        } else if (v.getId() == R.id.btn_menu_report) {
//            startActivity(LearningActivity.class);
        } else if (v.getId() == R.id.btn_exit_app) {
            BackgroundMusic.instance(this).stop();
            appActivityManager.finishAllActivity();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        BackgroundMusic.instance(this).stop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGameState.setStep(GameState.EGameStep.AppOpenedGameWait);
        startRepeatPromptGbSound();

        restartTagCheck();
    }

    private boolean isWaitTag = true;

    private void restartTagCheck() {
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                isWaitTag = true;
                startTagCheck();
            }
        }, 1000);
    }

    /* 7,8,9 = 1,2,3 */
    private void startTagCheck() {
        if (!isWaitTag) return;
        if (checkOption(currentTags, ETag._1)) {
            gotoLearningActivity();
        } else if (checkOption(currentTags, ETag._2)) {
            gotoGameStepSelectActivity();
        } else if (checkOption(currentTags, ETag._3)) {
//            isWaitTag = false;
//            btn_menu_report.shake();
        }

        if (isWaitTag) {
            DelayHandler.newHandler().delay(new Runnable() {
                @Override
                public void run() {
                    startTagCheck();
                }
            }, LoopDetectionMillis);
        }
    }

    private void gotoLearningActivity() {
        isWaitTag = false;
        btn_menu_learning.shake();
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                startActivity(LearningActivity.class);
            }
        }, 1000);
    }

    private void gotoGameStepSelectActivity() {
        isWaitTag = false;
        btn_menu_game.shake();
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                startActivity(GameStepSelectActivity.class);
            }
        }, 1000);
    }

}
